package com.itwang.controller;

import com.itwang.pojo.Dept;
import com.itwang.pojo.Result;
import com.itwang.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
*  部门管理Controller
* */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

//    @RequestMapping(value = "/depts",method = RequestMethod.GET)

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门数据
     * @return
     */
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");

        // 调用service查询部门数据
        List<Dept> deptList = deptService.list();

        return Result.success(deptList);
    }


    /**
     * 删除部门数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除部门:{}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 新增部门数据
     * @param dept
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("新增部门: {}", dept);
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id查找部门数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查找id为{}的部门", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 根据id修改部门名称
     * @param dept
     * @return
     */
    @PutMapping
    public Result updateById(@RequestBody Dept dept) {
        log.info("修改部门名称{}", dept);
        deptService.updateById(dept);
        return Result.success();
    }
}
